package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.heima.mapper.UserMapper;
import com.heima.pojo.*;
import com.heima.pojo.dto.LoginDto;
import com.heima.pojo.vo.LoginUserVo;
import com.heima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class UserServiceImpl  implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 登录所用两个方法：
     * @param dto
     * @return
     */
    @Override
    public LoginUserVo login(LoginDto dto) {

        if(dto == null){
            //参数异常
        }

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getLoginName,dto.getLoginName());
        User user = userMapper.selectOne(wrapper);

        if(user == null){ //代表用户不存在得

        }
        if(!user.getPassword().equals(dto.getPassword())){ //代表密码错误

        }
        //密码相等
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setUserId(user.getId());
        loginUserVo.setUserName(user.getUserName());
        loginUserVo.setRoleCode(user.getRoleCode());
        loginUserVo.setSuccess(true);
        loginUserVo.setMsg("登录成功");
        loginUserVo.setToken(UUID.randomUUID().toString());
        return loginUserVo;
    }
    @Override
    public boolean addGoodsClass(GoodsClassName goodsName) {

        try {
            if(userMapper.addGoodsClass(goodsName.getClassName()) > 1 ){
                return false;
            }
        } catch (Exception e) {
            throw new RuntimeException("添加失败，商品类型已存在");
        }
        return true;
    }

    @Override
    public PageBean selectUserList(UserQueryParam user) {
        PageHelper.startPage(user.getPageIndex(), user.getPageSize());
        List<UserPojo> userList = userMapper.selectUserList(user);
        PageInfo<UserPojo> info = new PageInfo<>(userList);

        return new PageBean(user.getPageIndex(),user.getPageSize(), (long) info.getPages(), info.getTotal(), info.getList());
    }

    @Override
    public List<Role> selectRoleList() {
        return userMapper.selectRoleList();
    }

    @Override
    public Boolean addUser(UserPojo user) {
       User userInfo = userMapper.selectUserInfo(user);

        if (userInfo == null){

            user.setRoleCode("100"+user.getRoleId());
            user.setSecret(user.getMobile()+"lkd");
            userMapper.insertUser(user);
            return true;
        }

        return false;
    }

    @Override
    public Boolean updateUser(int userId, UserPojo user) {
        //正常是需要判断的，但是这里就不做判断了，判断 是不是改自己？如果是，可以同名， 如果不是，不可以同名。
        user.setId(userId);
        user.setRoleCode("100"+user.getRoleId());
        user.setSecret(user.getMobile()+"lkd");
        int result = userMapper.updateUser(user);
        if (result > 0){
            return true ;
        }
        return false;
    }

    @Override
    public Boolean deleteUser(int userId) {
         int result = userMapper.deleteByUserId(userId);
        if (result > 0){
            return true ;
        }
        return false;
    }
}
